package com.misty.blog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.misty.blog.bean.param.blog.ColCountParam;
import com.misty.blog.service.BlogCollectionService;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.BlogCollection;
import com.misty.dao.mapper.BlogCollectionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 博客收藏表 服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-12-02
 */
@Service
public class BlogCollectionServiceImpl extends ServiceImpl<BlogCollectionMapper, BlogCollection> implements BlogCollectionService {

    @Resource
    private BlogCollectionMapper blogCollectionMapper;


    /**
     *
     * @Param
     * @return
     * @Description 判断是否收藏过
     * @Date
     **/
    @Override
    public Boolean judgeIfCollection(Integer blogId, Integer currentUserId) {

        QueryWrapper<BlogCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogCollection::getBlogId,blogId)
                .eq(BlogCollection::getUserId,currentUserId);
        BlogCollection blogCollection = blogCollectionMapper.selectOne(queryWrapper);
        if(ObjectUtil.isEmpty(blogCollection)){
            return false;
        }
        return true;
    }

    /**
     *
     * @Param
     * @return
     * @Description 删除对应收藏记录
     * @Date
     **/
    @Override
    public Result<?> cancelCollection(ColCountParam param, Integer currentUserId) {

        //删除对应收藏记录
        UpdateWrapper<BlogCollection> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BlogCollection::getBlogId,param.getBlogId())
                .eq(BlogCollection::getUserId,currentUserId);
        int remove = blogCollectionMapper.delete(updateWrapper);
        if(remove <= 0){
            return Result.error("删除对应收藏记录失败！");
        }
        return Result.success("删除对应收藏记录成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 添加收藏
     * @Date
     **/
    @Override
    public Result<?> addCollection(ColCountParam param, Integer currentUserId) {

        //增加对应收藏记录
        BlogCollection insert = new BlogCollection();
        insert.setBlogId(param.getBlogId())
                .setUserId(currentUserId);
        int save = blogCollectionMapper.insert(insert);
        if(save <= 0){
            return Result.error("添加收藏失败！");
        }
        return Result.success("添加收藏成功！");
    }
}
